package cn.com.pajx.pajx_visitor_v3.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedHashMap;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.api.Api;
import cn.com.pajx.pajx_visitor_v3.base.BaseMvpFragment;
import cn.com.pajx.pajx_visitor_v3.bean.EventMessage;
import cn.com.pajx.pajx_visitor_v3.bean.SchoolBean;
import cn.com.pajx.pajx_visitor_v3.mvp.presenter.LoadDataPresenterImpl;
import cn.com.pajx.pajx_visitor_v3.utils.AppConstant;
import cn.com.pajx.pajx_visitor_v3.utils.DeviceIdUtil;
import cn.com.pajx.pajx_visitor_v3.utils.SharePreferencesUtil;
import cn.com.pajx.pajx_visitor_v3.utils.ToastUtil;

/**
 * 绑定/切换学校的页面
 * create an instance of this fragment.
 */
@SuppressLint("NonConstantResourceId")
public class BindSchoolFragment extends BaseMvpFragment<LoadDataPresenterImpl> {

    @BindView(R.id.view_device)
    EditText viewDeviceNo;
    @BindView(R.id.view_admin)
    EditText viewAdminPassword;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_change_title)
    TextView tvChangeTitle;
    @BindView(R.id.tv_bind)
    TextView tvBind;

    private String device_no, admin_password;
    private boolean isChange;

    public BindSchoolFragment() {
    }

    public static BindSchoolFragment newInstance(boolean isChange) {
        BindSchoolFragment fragment = new BindSchoolFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(AppConstant.CHANGE_SCHOOL, isChange);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
        Objects.requireNonNull(getDialog()).setOnKeyListener((dialog, keyCode, event) -> keyCode == KeyEvent.KEYCODE_BACK);
        return super.onCreateView(inflater, container, savedInstanceState);

    }

    @Override
    protected void beforeInitHead() {
        super.beforeInitHead();
        Bundle bundle = getArguments();
        if (bundle != null) {
            isChange = bundle.getBoolean(AppConstant.CHANGE_SCHOOL, false);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_bind_school;
    }

    @Override
    protected double loadWidthScale() {
        return 0.55;
    }

    @Override
    protected LoadDataPresenterImpl initPresenter() {
        return new LoadDataPresenterImpl();
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        ivClose.setVisibility(isChange ? View.VISIBLE : View.INVISIBLE);
        tvChangeTitle.setText(isChange ? "切换学校" : "绑定学校");
        tvBind.setText(isChange ? "确认切换" : "确认绑定");
        initTextChangedListener();
    }

    /**
     * 事件监听
     */
    @SuppressLint("SetTextI18n")
    private void initTextChangedListener() {
        //设备编号
        viewDeviceNo.setText("13100002000003");
        viewAdminPassword.setText("888888");
    }

    /**
     * 加载拜访的预约信息
     */
    private void bindSchool() {
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("equ_no", device_no);
        map.put("pw_admin", admin_password);
        map.put("dev_id", DeviceIdUtil.getDeviceIMEI(mContext));
        mPresenter.loadData(Api.BIND_SCHOOL, map, "BIND_SCHOOL", "正在加载");
    }

    @OnClick({R.id.tv_bind, R.id.iv_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_bind://绑定
                if (checkParams()) return;
                hideSoftInput();
                //绑定数据
                bindSchool();
                break;
            case R.id.iv_close://关闭
                dismiss();
                break;
        }
    }

    /**
     * 检验参数
     *
     * @return 布尔值 true有空数据 false 通过
     */
    private boolean checkParams() {
        device_no = viewDeviceNo.getText().toString().trim();
        admin_password = viewAdminPassword.getText().toString().trim();
        if (TextUtils.isEmpty(device_no)) {
            ToastUtil.toast("请输入设备编号");
            return true;
        }
        if (TextUtils.isEmpty(admin_password)) {
            ToastUtil.toast("请输入管理密码");
            return true;
        }
        return false;
    }

    @Override
    public void onSuccess(String data, String aesKey, String message, int code, String tag) {
        super.onSuccess(data, aesKey, message, code, tag);
        SchoolBean schoolBean = new Gson().fromJson(data, SchoolBean.class);
        if (schoolBean != null) {
            SharePreferencesUtil.getInstance().putString(AppConstant.SCHOOL_BEAN, data);
            SharePreferencesUtil.getInstance().putBoolean(AppConstant.FIRST_INSTALL, false);
        }
        //切换学校
        EventBus.getDefault().post(new EventMessage(AppConstant.CHANGE_SCHOOL));
        //关闭页面
        dismiss();

    }
}